import { defineStore } from "pinia"
import { loginAPI } from "@/api/login"
import { ElMessage } from "element-plus"
import { ref } from "vue"

// 用户信息仓库
export const useUserStore = defineStore(
  "user",
  () => {
    // 用户token
    const token = ref<string>("")

    // 登录
    const login = async (data: any) => {
      const res = await loginAPI(data)
      if (res.code === 1) {
        // 保存token
        token.value = res.data.token
        ElMessage.success("登录成功")
        return true
      } else {
        ElMessage.error(res.msg)
        return false
      }
    }
    // 退出登录
    const logout = () => {
      token.value = ""
    }
    // 店铺状态
    const shopStatus = ref<number>(0)
    // 设置店铺状态
    const setShopStatus = (status: number) => {
      shopStatus.value = status
    }
    return {
      token,
      login,
      logout,
      shopStatus,
      setShopStatus
    }
  },
  {
    persist: true
  }
)
